<div class="alert-title">添加在线充值</div>

<table class="charge-info">
    <tr>
        <td class="title">排序</td>
        <td class="field">
            <input type="number" class="admin_input" name="sort" placeholder="数字越大越靠前"/>
        </td>
    </tr>
    <tr>
        <td class="title">商户类型</td>
        <td class="field">
            <select data-am-selected="{maxHeight: 250}" name="type">
                <option value="heyifuu">合易付</option>
                <option value="sslsf">闪付</option>
                <option value="xinmapay">新码</option>
            </select>
        </td>
    </tr>
    <tr>
        <td class="title">支付方式</td>
        <td class="field">
            <select data-am-selected="{maxHeight: 250}" name="payType">
                <option value="bank">网银</option>
                <option value="alipay">支付宝扫码</option>
                <option value="wechat">微信扫码</option>
                <option value="qq">QQ扫码</option>
            </select>
        </td>
    </tr>
        
    <tr>
        <td class="title">前端展示描述</td>
        <td class="field">
            <input type="text" class="admin_input" name="display" placeholder="请输入在前台显示的充值名称"/>
        </td>
    </tr>
    
    <tr>
        <td class="title">商户账号</td>
        <td class="field">
            <input type="text" class="admin_input" name="merchNo" placeholder="请输入商户账号"/>
        </td>
    </tr>
    
    <tr>
        <td class="title">商户ID</td>
        <td class="field">
            <input type="text" class="admin_input" name="appid" placeholder="请输入商户ID"/>
        </td>
    </tr>
    
    <tr>
        <td class="title">秘钥</td>
        <td class="field">
            <input type="text" class="admin_input" name="secret" placeholder="请输入商户秘钥"/>
        </td>
    </tr>
    
    <tr>
        <td class="title">充值回调地址</td>
        <td class="field">
            <input type="text" class="admin_input" name="url" placeholder="请输入第三方充值推送的接收网址"/>
        </td>
    </tr>
    
    <tr class="sf" style="display:none">
        <td class="title">充值域名</td>
        <td class="field">
            <input type="text" class="admin_input" name="payUrl" placeholder="请输入充值的网址"/>
        </td>
    </tr>
    
    <tr>
        <td class="title">报警额度</td>
        <td class="field">
            <input type="number" class="admin_input" name="warning"/>
        </td>
    </tr>
    
    <tr>
        <td class="title">手续费</td>
        <td class="field">
            <input type="number" class="admin_input" name="fee" placeholder="示例：百分一请填写 0.01"/>
        </td>
    </tr>
    
    <tr>
        <td class="title">最小充值金额</td>
        <td class="field">
            <input type="number" class="admin_input" name="min" placeholder=""/>
        </td>
    </tr>

    <tr>
        <td class="title">最大充值金额</td>
        <td class="field">
            <input type="number" class="admin_input" name="max" placeholder=""/>
        </td>
    </tr>
    
    <tr>
        <td class="title">充值层级</td>
        <td class="field charge-layers">
        </td>
    </tr>

    <tr>
        <td class="title">是否启用</td>
        <td class="field charge-enable">
            <label class="am-checkbox"><input type="checkbox" class="enable" value="" data-am-ucheck> 启用</label>
        </td>
    </tr>
</table>

<div class="button_line">
    <button class="btn-save common_button admin_button">保存</button>
    <button class="close common_button admin_button">关闭</button>
</div>
<style type="text/css">
    .charge-info { width: 94%; margin-left: 3%; }
    .charge-info td { padding: 5px 0px 5px 0px; }
    .charge-info .title {
        width: 100px;
        text-align: right;
        padding-right: 10px;
    }
</style>

<script>
    $(".button_line .close").click(function() {
        $("#add_charge_type").remove();
    });

    if (context.center.chargeOper == 'edit') {
        $(".alert-title").html("编辑");
        var d = context.center.chargeInfo;
        $(".charge-info input[name='sort']").val(d.sort || 0);
        $(".charge-info select[name='type']").val(d.type);
        $(".charge-info select[name='payType']").val(d.payType || '');
        $(".charge-info select[name='payType']").trigger('changed.selected.amui');
        $(".charge-info select[name='type']").trigger('changed.selected.amui');

        $(".charge-info input[name='display']").val(d.name);
        $(".charge-info input[name='secret']").val(d.signKey);
        $(".charge-info input[name='url']").val(d.noticeUrl);
        $(".charge-info input[name='warning']").val(d.warning);
        $(".charge-info input[name='min']").val(d.min);
        $(".charge-info input[name='max']").val(d.max);
        $(".charge-info input[name='fee']").val((d.fee || 0));
        $(".charge-info input[name='appid']").val(d.appId);
        $(".charge-info input[name='merchNo']").val(d.mchId);

        if (d.type == 'sslsf') {
            $(".charge-info .sf").show();
            $(".sf input[name='payUrl']").val(d.payUrl || '');
        }

        if (d.enable == 1) $(".charge-info .charge-enable input").uCheck('check');
        else $(".charge-info .charge-enable input").uCheck('uncheck');
    }
    $(".charge-info select[name='type']").change(function() {
        if ($(".charge-info select[name='type']").val() == 'sslsf') {
            $(".charge-info .sf").show();
        }
        else {
            $(".charge-info .sf").hide();
        }
    })

    Layer.query(undefined, function(r) {
        var layers = Layer.sort(r);
        if (context.center.chargeOper == 'edit')
            $(".charge-layers").append(genGroups(context.center.chargeInfo.layer || '', layers));
        else
            $(".charge-layers").append(genGroups('', layers));
    });

    // 用户组复选框
    function genGroups(roles, layers) {
        roles = roles.split(",");
        var td = $("<div></div>");
        for (var i in layers) {
            var line = '<label class="am-checkbox"><input type="checkbox" value="__v__" data-am-ucheck __c__> __t__</label>';
            line = line.replace(/__v__/, layers[i].level);
            line = line.replace(/__t__/, layers[i].name);
            if (roles.indexOf(layers[i].level + "") == -1) line = line.replace(/__c__/, '');
            else line = line.replace(/__c__/, 'checked');
            td.append(line);
        }
        return td;
    }

    $(".button_line .btn-save").click(function() {
        var data = {
        };
        if (context.center.chargeOper == 'edit') {
            data.id = context.center.chargeInfo.id;
            data.key = context.center.chargeInfo.key;
        }
        else data.key = Util.uuid();
        var v = $(".charge-info select[name='type']").val();
        data.type = v;
        var v = $(".charge-info select[name='payType']").val();
        data.payType = v;

        var v = $(".charge-info input[name='display']").val().trim();
        if (!v) {
            alert2("请输入前端展示描述", true); return;
        }
        data.name = v;

        var v = $(".charge-info input[name='appid']").val().trim();
        if (!v) {
            alert2("请输入商户ID", true); return;
        }
        data.appId = v;

        var v = $(".charge-info input[name='merchNo']").val().trim();
        if (!v) {
            alert2("请输入商户账号", true); return;
        }
        data.mchId = v;

        var v = $(".charge-info input[name='secret']").val().trim();
        if (!v) {
            alert2("请输入秘钥", true); return;
        }
        data.signKey = v;

        var v = $(".charge-info input[name='url']").val().trim();
        if (!v) {
            alert2("请输入充值地址", true); return;
        }
        data.noticeUrl = v;

        var v = $(".charge-info input[name='warning']").val().trim();
        data.warning = v;

        var v = $(".charge-info input[name='fee']").val().trim();
        data.fee = v;

        var v = $(".charge-info input[name='min']").val().trim();
        data.min = parseInt(v || 0);
        var v = $(".charge-info input[name='max']").val().trim();
        data.max = parseInt(v || 0);

        var v = $(".charge-info .charge-enable input")[0].checked;
        data.enable = v ? 1 : 0;

        var v = $(".charge-info input[name='sort']").val();
        data.sort = v || 0;

        if (data.type == 'sslsf') {
            data.payUrl = $(".sf input[name='payUrl']").val().trim();
        }

        data.layer = [];
        var arr2 = $(".charge-layers input[type='checkbox']:checked");
        for (var j = 0; j < arr2.length; j++) {
            data.layer.push($(arr2[j]).val());
        }
        data.layer = data.layer.join(",");

        var key = Util.uuid();
        if (context.center.chargeOper == 'edit') key = context.center.chargeInfo.key;

        // 保存
        CMD.pay_save(data, function() {
            $("#add_charge_type").remove();
            _updateChargeList();
        });
    });
</script>
